#ifndef HFT_ORDER_MANAGER_H
#define HFT_ORDER_MANAGER_H

#include "execution/execution_types.h"
#include <string>
#include <vector>
#include <unordered_map>
#include <chrono>
#include <functional>
#include <iomanip>
#include <sstream>

namespace hft {

class OrderManager {
public:
    OrderManager();
    
    std::string createOrder(const Order& order);
    bool modifyOrder(const std::string& order_id, double price, int volume);
    bool cancelOrder(const std::string& order_id);
    int cancelOrders(const std::vector<std::string>& order_ids);
    int cancelAllOrders();
    int cancelSymbolOrders(const std::string& symbol);
    int cancelAccountOrders(const std::string& account);
    int cancelStrategyOrders(const std::string& strategy_id);
    
    void onOrderStatus(const Order& order);
    void onTrade(const Trade& trade);
    
    Order* getOrder(const std::string& order_id);
    std::vector<Order*> getAccountOrders(const std::string& account);
    std::vector<Order*> getStrategyOrders(const std::string& strategy_id);
    std::vector<Order*> getSymbolOrders(const std::string& symbol);
    const std::unordered_map<std::string, Order>& getAllOrders() const;
    std::vector<Order*> getActiveOrders();
    
    void cleanCompletedOrders();
    
    void registerOrderCallback(std::function<void(const Order&)> callback);
    void registerTradeCallback(std::function<void(const Trade&)> callback);

private:
    std::unordered_map<std::string, Order> orders_;
    std::unordered_map<std::string, Trade> trades_;
    std::function<void(const Order&)> order_callback_;
    std::function<void(const Trade&)> trade_callback_;
    int order_id_counter_ = 0;
    
    std::string generateOrderId();
    void notifyOrderUpdate(const Order& order);
    void notifyTrade(const Trade& trade);
};

} // namespace hft

#endif // HFT_ORDER_MANAGER_H